﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;

namespace Netcare.Core
{
    public class ConsoleLogListener : LogListener
    {
        private TextWriter writer;

        public ConsoleLogListener()
        {
        }

        public override void WriteLine(string level, string text)
        {
            if (EnsureWriter())
            {
                writer.WriteLine(String.Format("[{0}][{1}] {2}",
                    DateTime.Now.ToString("HH:mm:ss.fff"), level, text));
            }
        }

        public override void Flush()
        {
            if (this.writer != null)
            {
                this.writer.Flush();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.Close();
            }
        }

        protected override void Close()
        {
            if (this.writer != null)
            {
                this.writer.Close();
            }
            this.writer = null;
        }

        private bool EnsureWriter()
        {
            bool flag = true;
            if (this.writer == null)
            {
                flag = false;
                writer = Console.Out;
                flag = true;
            }
            return flag;
        }


    }
}
